$(document).ready(function(){
    $("li a#login").hover(function(){
         $(".menu").css("background","url("+path+"/images/selected.gif)");
    },function(){
         if(!$("li a#login").hasClass("active")){
                    $(".menu").css("background","#520000");}
    });

$("#login").click(function(){
        $.ajax({type:"GET",url:path+"process",cache:true,success:function(html){
            if(html == "Y"){goToPrivate();}
           else{$("#login_div").html(html);
                     $('#login_form').modal();}
        }});
   });

    $("#sendmail").click(function(){
                var valid =true;
                var recipient = $.trim($("#recipient").val());
		var name = $.trim($("#name").val());
		var mail =$("#mail").val();
		var subject = $.trim($("#subject").val());
		var emailBody = $.trim($("#emailBody").val());
                if (recipient.length<1) {
                        $("#recipienterror").html("Recipient is required");
                        valid= false;
		} else{
                    $("#recipienterror").html("");
                }
		if (name.length<1) {
                        $("#nameerror").html("Name is required");
                        valid= false;
		} else{
                    $("#nameerror").html("");
                }
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
                     $("#mailerror").html("Email  is not valid");
                        valid= false;
		} else{
                     $("#mailerror").html("");
                }
		if (subject.length<1) {
                     $("#subjecterror").html("Subject is required");
                        valid= false;
		} else{
                    $("#subjecterror").html("");
                }
		if (emailBody.length<1) {
                     $("#emailBodyerror").html("Email body is required");
                        valid= false;
		}else{
                    $("#emailBodyerror").html("");
                }
                if(!valid){
                    return false;
                }
		else {
                    $("#result").html("<center><img src='"+path+"images/loading.gif'/>&nbsp;&nbsp;Sending Message </center>");
                   $("form#mailform").submit();
		}
		return false;
	});
 
});
// Preload Images
img1 = new Image(16, 16);
img1.src="images/spinner.gif";

img2 = new Image(220, 19);
img2.src="images/ajax-loader.gif";


function login(){
      var mail = $("#emailAddress").val();
        if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
        $("#login_response").html("Email Address  is not valid");
        return false;
    }
     if($.trim($("#password").val())==""){
        $("#login_response").html("Password is required");
        return false;
    }
   $('#login_button').hide();
   $('#ajax_loading').show();

   $.ajax({
    type: "POST",
    url: "process/login",  // Send the login info to this page
    data: {emailAddress:mail,password:$("#password").val(),role:$("#role").val()},
    success: function(msg){
        if(msg=="OK"){goToPrivate();}
         else{$("#login_response").html(msg);
         $('#login_button').show();
         $('#ajax_loading').hide();}
    }
  });
}

function goToPrivate(){
  window.location="process/private";
}
